package com.pignest.hotel.ui.reception;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;

import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Toolbarbutton;

import org.zkoss.zul.Window;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.TbAccounts;
import com.pignest.hotel.beans.TbGoods;
import com.pignest.hotel.beans.TbLiveGoodsBorrow;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;


public class CheckBorrowWindow extends Window implements EventListener {

	/**
	 * 查看物品借用情况，如果有丢失或者损坏，则照价赔
	 */

	private static final long serialVersionUID = 4669460806925673067L;
	private ReceptionService receptionService;
	
	public Living living;
	private Listbox goodsBorrowlistbox;
	private Listbox payReasionListbox;
	private Toolbarbutton backbtn; 
	private Toolbarbutton payGoodsbtn;

	private boolean success;
	
	public CheckBorrowWindow() {
		receptionService = Services.getReceptionService();
		addEventListener("onCreate", this);
	}
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			goodsBorrowlistbox=(Listbox)getFellow("goodsBorrowlistbox");
			payReasionListbox=(Listbox)getFellow("payReasionListbox");
			backbtn=(Toolbarbutton)getFellow("backbtn");
			payGoodsbtn=(Toolbarbutton)getFellow("payGoodsbtn");
			
			query();
		}
	}
	public void query(){
		goodsBorrowlistbox.getItems().clear();
		StringBuffer hql=new StringBuffer();
		if(this.living!=null){
			 hql.append(" from TbLiveGoodsBorrow b where b.living.id='"+living.getId()+"'");
			 List <TbLiveGoodsBorrow> goodsBorrowList=receptionService.find(hql.toString());
			    for(TbLiveGoodsBorrow s:goodsBorrowList){
			    	Listitem listitem = new Listitem();
			    	listitem.setValue(s.getId());
			    	listitem.setParent(goodsBorrowlistbox);
			    	listitem.appendChild(new Listcell(s.getTbGoods().getName()));
			    	listitem.appendChild(new Listcell(DateUtil.toFullString(s.getTime())));
			    	listitem.appendChild(new Listcell(DateUtil.toFullString(s.getBacktime())));
			    	final Date backtime =s.getBacktime();
			    	Integer i=s.getCount();
			    	if(i!=null)
			    	   listitem.appendChild(new Listcell(String.valueOf(s.getCount())));
			    	else
			    		listitem.appendChild(new Listcell(""));
			    	listitem.appendChild(new Listcell(MoneyUtil.toString(s.getTbGoods().getPrice())));
			    	listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
						public void onEvent(Event event) throws Exception {   
							
							if(backtime!=null){
								backbtn.setDisabled(true);
								payGoodsbtn.setDisabled(true);
							}else{
								backbtn.setDisabled(false);
								payGoodsbtn.setDisabled(false);
							}
								
							
						}
					});
			    }
		}
		
	}
	/**
	 * 设定归还时间
	 * **/
	public String checkBack(){
	 try{
		String [] ids =UIUtils.getSeclectListId(goodsBorrowlistbox);
		receptionService.updateBorrowGoodsBackTime(ids);
		return "";
		}catch(Exception e){
			e.printStackTrace();
			return "异常";
		}
	}
	/***
	 * 设定照价赔偿
	 * 
	 * **/
	public String checkPayGoods(){
		try{
			String [] ids =UIUtils.getSeclectListId(goodsBorrowlistbox);
			String payReason=UIUtils.getSelectedValue(payReasionListbox);
			String re=receptionService.insertProjects(ids, living, payReason);
			return re;
		}catch(Exception e){
			e.printStackTrace();
			return "异常"+e.getMessage();
		}
	}
	public Living getLiving() {
		return living;
	}
	public void setLiving(Living living) {
		this.living = living;
	}
	
}
